package com.augusaime.boot.controller;

import org.springframework.lang.UsesSunHttpServer;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@Controller
public class RequestController {

  @GetMapping("/goto")
  public String geToPage(HttpServletRequest request) {
    request.setAttribute("msg", "成功了...");
    request.setAttribute("code", 200);
    return "forward:/success";// 转发到 /success请求
  }

  @GetMapping("/params")
  public String testParam(Map<String, Object> map,
                          Model model,
                          HttpServletRequest request,
                          HttpServletResponse response) {
    map.put("hello", "world666");
    model.addAttribute("world", "hello666");
    request.setAttribute("message", "HelloWorld");

    Cookie cookie = new Cookie("c1", "v1");
    cookie.setDomain("localhost");
    response.addCookie(cookie);

    return "forward:/success";

  }

  @ResponseBody
  @GetMapping("/success")
  public Map success(@RequestAttribute(value = "msg", required = false) String msg,
                     @RequestAttribute(value = "code", required = false) Integer code,
                     HttpServletRequest request) {
    Object msg1 = request.getAttribute("msg");

    Map<String, Object> map = new HashMap<>();
    Object hello = request.getAttribute("hello");
    Object world = request.getAttribute("world");
    Object message = request.getAttribute("message");


    map.put("reqMethod_msg", msg1);
    map.put("annotation_msg", msg);
    map.put("hello", hello);
    map.put("world", world);
    map.put("message", message);


    return map;
  }


}
